﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Intelligent_Dapeng
{
    public partial class Form1 : Form
    {
        ConnServer connServer = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            connServer = new ConnServer("139.198.108.100", 3389);
            
            chart1.Series.Clear();
            ChartHelper.AddSeries(chart1, "温度", SeriesChartType.SplineRange, Color.FromArgb(200, 255, 0, 40), Color.FromArgb(0, 0, 0, 0));
            ChartHelper.AddSeries(chart1, "湿度", SeriesChartType.SplineRange, Color.FromArgb(100, 0, 255, 120), Color.FromArgb(0, 0, 0, 0));
            ChartHelper.SetTitle(chart1, "湿度与温度统计图", new Font("黑体", 12), Docking.Bottom, Color.White);
            ChartHelper.SetStyle(chart1, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chart1, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);
            ChartHelper.SetXY(chart1, "时间", "摄氏度--百分比", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 60, 10);
            ChartHelper.SetMajorGrid(chart1, Color.Gray, 20000, 10);

            chart3.Series.Clear();
            ChartHelper.AddSeries(chart3, "光照强度", SeriesChartType.SplineRange, Color.FromArgb(100, 0, 0, 255), Color.Red);
            ChartHelper.SetTitle(chart3, "光照强度统计图", new Font("黑体", 12), Docking.Bottom, Color.White);
            ChartHelper.SetStyle(chart3, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chart3, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);
            ChartHelper.SetXY(chart3, "时间", "强度", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 60, 10);
            ChartHelper.SetMajorGrid(chart3, Color.Gray, 20000, 10);

            for (int i = 1; i <= 60 * 10; i++)
            {
                x1.Add(-(60 * 10 - i));
            }
        }

        List<int> x1 = new List<int>();
        List<int> y1 = new List<int>();
        List<int> y2 = new List<int>();
        List<int> y3 = new List<int>();
        bool The_one=false;

        public void RefreshData()
        {

            if (connServer.nowdata_sta && The_one==false)
            { 
                connServer.nowdata_sta = false;
                The_one = true;
                numericUpDown1.Value = connServer.temperature_thr;
                numericUpDown2.Value = connServer.humidity_thr;
                numericUpDown3.Value = connServer.illumination_thr;
            }

            if (connServer.tempdata_sta)
            {
                connServer.tempdata_sta = false;

                y1.Clear();

                for (int i = 0; i < 600; i++)
                {
                    y1.Add(connServer.temperature[i]);
                }

                RefreshChart(x1, y1, 1);
            }

            if (connServer.humidata_sta)
            {
                connServer.humidata_sta = false;

                y2.Clear();

                for (int i = 0; i < 600; i++)
                {
                    y2.Add(connServer.humidity[i]);
                }

                RefreshChart(x1, y2, 2);
            }


            if (connServer.illudata_sta)
            {
                connServer.illudata_sta = false;

                y3.Clear();

                for (int i = 0; i < 600; i++)
                {
                    y3.Add(connServer.illumination[i]);
                }

                RefreshChart(x1, y3, 3);
            }
        }

        public delegate void RefreshChartDelegate(List<int> x, List<int> y, int type);

        public void RefreshChart(List<int> x, List<int> y, int type)
        {
            if (type == 1)
            {
                if (this.chart1.InvokeRequired)
                {
                    RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);
                    this.Invoke(stcb, new object[] { x, y, type });
                }
                else
                {
                    chart1.Series[0].Points.DataBindXY(x, y);
                }
            }
            else if (type == 2)
            {
                if (this.chart1.InvokeRequired)
                {
                    RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);
                    this.Invoke(stcb, new object[] { x, y, type });
                }
                else
                {
                    chart1.Series[1].Points.DataBindXY(x, y);
                }
            }
            else if (type == 3)
            {
                if (this.chart3.InvokeRequired)
                {
                    RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);
                    this.Invoke(stcb, new object[] { x, y, type });
                }
                else
                {
                    chart3.Series[0].Points.DataBindXY(x, y);
                }
            }
        }

        int conn_cnt=0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(RefreshData)).Start();

            if (connServer != null)
            {
                if (connServer.conn_Sta == Conn_sta.Disconnect)
                {
                    label4.Text = "服务器断开连接" + conn_cnt;
                    label4.ForeColor = Color.FromArgb(255, 0, 0);
                    if (conn_cnt == 0)
                    {
                        conn_cnt = 3;
                        connServer.Conn();
                    }
                    conn_cnt--;
                }
                else if (connServer.conn_Sta == Conn_sta.Connecting)
                {
                    label4.Text = "正在连接服务器";
                    label4.ForeColor = Color.FromArgb(0, 0, 255);
                }
                else if (connServer.conn_Sta == Conn_sta.connect)
                {
                    label4.Text = "服务器连接成功";
                    label4.ForeColor = Color.FromArgb(0, 255, 0);

                    if (The_one == true)//第一次接收到阈值数据前   拒绝发送
                    {
                        connServer.Send_dat((int)numericUpDown1.Value,
                                            (int)numericUpDown2.Value,
                                            (int)numericUpDown3.Value,
                                            (int)heating_mode,
                                            (int)humidification_mode,
                                            (int)Filllight_mode);
                    }
                }
            }
        }

        int heating_mode = 10;
        int humidification_mode = 10;
        int Filllight_mode = 10;

        private void button2_Click(object sender, EventArgs e)
        {
            if (heating_mode < 10)
            {
                if (heating_mode == 1)
                {
                    heating_mode = 0;
                    groupBox1.Text = "加热-手动模式-关闭";
                    button2.Text = "打开";
                }
                else
                {
                    heating_mode = 1;
                    groupBox1.Text = "加热-手动模式-打开";
                    button2.Text = "关闭";
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (heating_mode < 10)
            {
                heating_mode += 10;
                groupBox1.Text = "加热-自动模式";
                button2.Text = "自动中";
                button1.Text = "手动";
            }
            else
            {
                button1.Text = "自动";
                heating_mode -= 10;
                if (heating_mode == 0)
                {
                    heating_mode = 0;
                    groupBox1.Text = "加热-手动模式-关闭";
                    button2.Text = "打开";
                }
                else
                {
                    heating_mode = 1;
                    groupBox1.Text = "加热-手动模式-打开";
                    button2.Text = "关闭";
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (humidification_mode < 10)
            {
                if (humidification_mode == 1)
                {
                    humidification_mode = 0;
                    groupBox2.Text = "加湿-手动模式-关闭";
                    button3.Text = "打开";
                }
                else
                {
                    humidification_mode = 1;
                    groupBox2.Text = "加湿-手动模式-打开";
                    button3.Text = "关闭";
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (humidification_mode < 10)
            {
                humidification_mode += 10;
                groupBox2.Text = "加湿-自动模式";
                button3.Text = "自动中";
                button4.Text = "手动";
            }
            else
            {
                humidification_mode -= 10;
                button4.Text = "自动";
                if (humidification_mode == 0)
                {
                    humidification_mode = 0;
                    groupBox2.Text = "加湿-手动模式-关闭";
                    button3.Text = "打开";
                }
                else
                {
                    humidification_mode = 1;
                    groupBox2.Text = "加湿-手动模式-打开";
                    button3.Text = "关闭";
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (Filllight_mode < 10)
            {
                if (Filllight_mode == 1)
                {
                    Filllight_mode = 0;
                    groupBox3.Text = "补光-手动模式-关闭";
                    button5.Text = "打开";
                }
                else
                {
                    Filllight_mode = 1;
                    groupBox3.Text = "补光-手动模式-打开";
                    button5.Text = "关闭";
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (Filllight_mode < 10)
            {
                Filllight_mode += 10;
                groupBox3.Text = "补光-自动模式";
                button5.Text = "自动中";
                button6.Text = "手动";
            }
            else
            {
                Filllight_mode -= 10;
                button6.Text = "自动";
                if (Filllight_mode == 0)
                {
                    Filllight_mode = 0;
                    groupBox3.Text = "补光-手动模式-关闭";
                    button5.Text = "打开";
                }
                else
                {
                    Filllight_mode = 1;
                    groupBox3.Text = "补光-手动模式-打开";
                    button5.Text = "关闭";
                }
            }
        }
    }
}
